package org.test.springAOP.ExampleError;

import java.io.PrintStream;
import java.io.PrintWriter;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

@Aspect
@Repository("TheAspectErrorExample")
public class TheAspect {

//	/**
//	 * 方法前
//	 */
//	@Before("execution(* org.test..*(..))")
//	public void before() {
//		System.out.println("这是spring beforeMethod");
//	}
//
//	/**
//	 * 返回值后，retVal为返回值
//	 * 
//	 * @param retVal
//	 */
//	@AfterReturning(pointcut = "execution(* org.test..*(..))", returning = "retVal", value = "vl")
//	public void afterReturn(Object retVal) {
//		System.out.println("这是spring aop afterReturn ");
//		System.out.println("返回值为:" + retVal.toString());
//		System.out.println("afterReturn");
//	}
//
//	/**
//	 * 这个可以从joinpoint中拿到类，方法，参数,注意，最后要 return joinpoint.proceed();
//	 */
//	@Around("execution(* org.test..*(..))")
//	public Object audit(ProceedingJoinPoint joinpoint) throws Throwable {
//		System.out.println("这是spring aop around");
//		Object[] params = joinpoint.getArgs();
//		for (int i = 0; i < params.length; i++) {
//		System.out.println(params[i]);
//		}
//		System.out.println(joinpoint.getSignature());
//		return joinpoint.proceed();
//	}

	@AfterThrowing(pointcut = "execution(* org.test..*(..))",
			throwing = "ex")
	public void doRecoveryActions(Exception ex) {
		System.out.println("这是spring afterThrowing begin");
		/**
		 *输出类java.lang.IndexOutOfBoundsException的
		 */
		System.out.println(ex.getClass().getCanonicalName()+"**");
		
		/**
		 * 下面的为详细的错误
		 */
		StringBuffer stringBuffer = new StringBuffer();
		  StackTraceElement[] trace = ex.getStackTrace();
		  for (int i=0; i < trace.length; i++)
		  {
              stringBuffer.append("\n\tab " + trace[i]);
		  }
		  System.out.println(stringBuffer.toString());
		
		System.out.println("这是spring afterThrowing end");
	}

}